/**
 *
 * @author 6g3y
 */
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Process {
    public static String createAsciiPicString(String path,int sizex) {
        String base = "@#$%&MW8BmwqpdbkhaoQ*0OZXYUJCLtfjzxnuvcr[]{}1()|/?Il!i><+-;;;,;,;,,,,,.... ";
        //base="@#$%&MW8BmwqpdbkhaoQ*0OZXYUJCLtfjzxnuvcr[]{}1()|/?Il!i><+-;,. ";
        StringBuilder retval=new StringBuilder();//builder比buffer快，多线程不安全
        try {
            BufferedImage image = ImageIO.read(new File(path));
            //int dp = Math.max(image.getHeight(), image.getWidth());
            int sizey=image.getHeight()*sizex/image.getWidth();
            for (int yy = 0; yy <sizey-2 ; yy+=2) {
                for (int xx = 0; xx < sizex-1; xx ++) {
                    int x=image.getWidth()-(int)((1-(((float)xx)/sizex))*image.getWidth());
                    int y=image.getHeight()-(int)((1-(((float)yy)/sizey))*image.getHeight());
                    //System.out.println(x+" "+y+" "+image.getWidth()+ " "+image.getHeight());
                    int pixel = image.getRGB(x, y);
                    int r = (pixel & 0xff0000) >> 16, g = (pixel & 0xff00) >> 8, b = pixel & 0xff;
                    float gray = 0.299f * r + 0.578f * g + 0.114f * b; //https://en.wikipedia.org/wiki/Grayscale 灰度值计算公式
                    int index = Math.round(gray * (base.length()+base.length()/6 ) / 255);
                    retval.append(index >= base.length() ? " " : base.charAt(index));
                    //System.out.print(index >= base.length() ? " " : base.charAt(index));
                }
                retval.append("\r\n");
                //System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return retval.toString();
    }
    public static int [] createColorIntegerArray(String path,int sizex) {    
        return null;
    }

//    public static void main(final String[] args) {
//        new Main().createAsciiPic("5.jpg");
//    }
    
}

